<template>
	<view class="container">
		<view class="address-wrapper">
			<view class="address-item" v-for="address in addressList" @click="checkedAddress(address)"
				:key="address.id">
				<view class="text">
					<view class="address">
						{{ address.provinceName }}{{ address.cityName }}{{ address.districtName }}{{ address.address }}
						<text class="tag" v-if="address.isDefault == 1">默认</text>
					</view>
					<view class="name-tel">{{ address.name }} {{ address.mobile }}</view>
				</view>
				<image src="/static/delet.png" class="btn" @click.stop="handleRemove(address.id)"></image>
				<image src="/static/edit.png" class="btn" @click.stop="details(address.id)"></image>
			</view>
		</view>
		<view class="bottom-menu" @click="details(0)">添加收货地址</view>
		<view class="place"></view>
	</view>
</template>

<script>
	import {
		frontList,
		remove
	} from '@/api/address.js';
	export default {
		data() {
			return {
				addressList: [],
				from: '' // user来自个人中心
			};
		},
		onLoad(option) {
			if (option.from) {
				this.from = option.from;
			}
		},
		onShow() {
			this.getAddressList();
		},
		methods: {
			checkedAddress(addressObj) {
				if (this.from == 'user') {
					return;
				}
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.addressObj = addressObj; //修改上一页data里面的sh参数值为100
				prevPage.$vm.ifAddressCanPut = 2; //修改上一页data里面的sh参数值为100
				uni.$emit('getAddress', addressObj);
				prevPage.$vm.isCheckedAddress = true; // 监听是否选择地址
				uni.navigateBack({
					//uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			},
			getAddressList() {
				frontList().then(respanse => {
					this.addressList = respanse.result;
				});
			},

			// 删除
			handleRemove(addressId) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除？',
					success: function(res) {
						if (res.confirm) {
							let pages = getCurrentPages(); //获取所有页面栈实例列表
							console.log(pages);
							let nowPage = pages[pages.length - 1]; //当前页页面实例
							let prevPage = pages[pages.length - 2]; //上一页页面实例
							if (prevPage.$vm.addressObj && prevPage.$vm.addressObj.id == addressId) {
								prevPage.$vm.isCheckedAddress = false;
							}
							remove({
								addressId
							}).then(respanse => {
								that.getAddressList();
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			details(id) {
				if (id) {
					uni.navigateTo({
						url: `/pages/address/details?id=${id}`
					});
				} else {
					uni.navigateTo({
						url: `/pages/address/details`
					});
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.address-item {
		display: flex;
		align-items: center;
		padding: 34rpx 15rpx 35rpx 25rpx;
		margin: 20rpx 25rpx;
		background: #ffffff;
		border-radius: 10rpx;

		.text {
			flex: 1;

			.address {
				line-height: 46rpx;
				font-size: 36rpx;
				font-weight: bold;
				color: #000000;

				.tag {
					display: inline-block;
					vertical-align: top;
					margin: 7rpx 0 0 15rpx;
					width: 58rpx;
					height: 31rpx;
					line-height: 31rpx;
					text-align: center;
					font-size: 22rpx;
					background: #25C477;
					border-radius: 10rpx;
					color: #fff;
				}
			}

			.name-tel {
				margin-top: 16rpx;
				line-height: 34rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}

		.btn {
			padding: 10rpx;
			margin-left: 20rpx;
			width: 34rpx;
			height: 34rpx;
		}
	}

	.bottom-menu {
		position: fixed;
		left: 25rpx;
		right: 25rpx;
		bottom: 79rpx;
		z-index: 2;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 32rpx;
		color: #fff;
		background: linear-gradient(90deg, #eb7c3f, #ea5d2e);
		border-radius: 45rpx;
	}

	.place {
		height: 169rpx;
		visibility: hidden;
	}
</style>
